package cn.edu.jxust.controller.api.enterprise;


import cn.edu.jxust.common.domain.entity.RegulationType;
import cn.edu.jxust.common.domain.valobj.RegulationTypeVo;
import cn.edu.jxust.common.domain.web.api.enterprise.IRegulationTypeController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.RegulationtypeService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouzhicheng
 * @since 2020-06-29
 */

@Slf4j
@RestController
public class RegulationTypeController implements IRegulationTypeController {

    @Resource
    private RegulationtypeService regulationtypeService;

    @Override
    public Result getAllRegulationType(Integer page, Integer size){

        try{
            IPage<RegulationType> pageInfo = regulationtypeService.page(new Page<>(page,size));
            if (pageInfo.getRecords() != null && !pageInfo.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, pageInfo);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "没有找到任何监管类型信息");
            }
        }catch (Exception e){
            log.error("RegulationTypeController --> getAllRegulationType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getOneRegulationType(Integer id){

        try{
            RegulationType byId = regulationtypeService.getById(id);
            if(byId != null){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, byId);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "没有找到任何监管类型信息");
            }
        }catch (Exception e){
            log.error("RegulationTypeController --> getOneRegulationType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result setOneRegulationType(RegulationTypeVo regulationTypeVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            RegulationType regulationType = new RegulationType();
            BeanUtil.copyProperties(regulationTypeVo ,regulationType);
            return regulationtypeService.save(regulationType)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "添加监管类型成功！")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "添加监管类型失败, 请重试!");
        }catch (Exception e){
            log.error("RegulationTypeController --> setOneRegulationType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result updateOneRegulationType(RegulationTypeVo regulationTypeVo, BindingResult result){

        if(result.hasErrors() || regulationTypeVo.getId() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try {
            RegulationType regulationType = new RegulationType();
            BeanUtil.copyProperties(regulationTypeVo ,regulationType);
            return regulationtypeService.updateById(regulationType)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS,"修改监管类型信息成功！")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED,"修改监管类型信息失败！");
        }catch (Exception e){
            log.error("RegulationTypeController --> updateOneRegulationType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result deleteOneRegulationType(Integer id) {

        try {
            return regulationtypeService.removeById(id)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "监管类型删除成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED,"监管类型删除失败！");
        }catch (Exception e){
            log.error("RegulationTypeController --> deleteOneRegulationType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}

